//
//  MineFollowListCell.m
//  ShowYork
//
//  Created by hxh on 2020/10/13.
//  Copyright © 2020 hxh. All rights reserved.
//

#import "MineFollowListCell.h"

@interface MineFollowListCell ()
@property (nonatomic,strong)UIButton *followBtn;
@property (nonatomic,strong)UIImageView *headIcon;
@property (nonatomic,strong)UILabel *nameLb;
@property (nonatomic,strong)UILabel *descLb;
@property (nonatomic,strong)CAGradientLayer *gradientLayer;

@property (nonatomic,strong)UIButton *coverBtn;
@end

@implementation MineFollowListCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self setUI];
    }
    return self;
}

- (void)setUI{
    
    self.coverBtn = [[UIButton alloc]init];
    [self.coverBtn addTarget:self action:@selector(coverMethod) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:self.coverBtn];
    
    [self.coverBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.bottom.equalTo(self.contentView);
    }];
    
    self.headIcon = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"defaut_header"]];
    self.headIcon.layer.cornerRadius = 30;
    self.headIcon.layer.masksToBounds = YES;
    [self.contentView addSubview:self.headIcon];
    
    self.nameLb = [[UILabel alloc]init];
    self.nameLb.text = @"Alejandro Hicks";
    self.nameLb.textColor = C51;
    self.nameLb.font = font(14);
    [self.contentView addSubview:self.nameLb];
    
    self.descLb = [[UILabel alloc]init];
    self.descLb.text = @"高考加油";
    self.descLb.textColor = C153;
    self.descLb.font = font(12);
    [self.contentView addSubview:self.descLb];
    
    self.followBtn = [[UIButton alloc]init];
    [self.followBtn setTitle:@"关注" forState:UIControlStateNormal];
    [self.followBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    self.followBtn.titleLabel.font = font(12);
    self.followBtn.backgroundColor = RGBA(224, 224, 224, 1);
    [self.followBtn addTarget:self action:@selector(followMethod) forControlEvents:UIControlEventTouchUpInside];
    self.followBtn.layer.cornerRadius = 13;
    self.followBtn.layer.masksToBounds = YES;
    [self.contentView addSubview:self.followBtn];
    
    self.gradientLayer = [CAGradientLayer layer];
    self.gradientLayer.frame = self.followBtn.bounds;
    self.gradientLayer.colors = @[(id)RGBA(247, 131, 97, 1).CGColor,(id)RGBA(245, 75, 100, 1).CGColor];
    [self.followBtn.layer insertSublayer:self.gradientLayer atIndex:0];
    
    [self.headIcon mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(60);
        make.height.mas_equalTo(60).priorityHigh();
        make.top.left.equalTo(self.contentView).offset(16);
        make.bottom.equalTo(self.contentView).offset(-16);
    }];
    
    [self.followBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.contentView);
        make.right.equalTo(self.contentView).offset(-16);
        make.size.mas_equalTo(CGSizeMake(80, 26));
    }];
    
    [self.nameLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.headIcon.mas_top).offset(8);
        make.left.equalTo(self.headIcon.mas_right).offset(16);
    }];
    
    [self.descLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.nameLb.mas_bottom).offset(8);
        make.left.equalTo(self.headIcon.mas_right).offset(16);
        make.right.equalTo(self.followBtn.mas_left).offset(-8);
    }];

}
- (void)coverMethod{
    if (self.delegate && [self.delegate respondsToSelector:@selector(showUserInfoListAtCurrentModel:)]) {
        [self.delegate showUserInfoListAtCurrentModel:self.model];
    }
}
- (void)followMethod{
    if (self.delegate && [self.delegate respondsToSelector:@selector(followAtCurrentModel:)]) {
        [self.delegate followAtCurrentModel:self.model];
    }
}
- (void)drawRect:(CGRect)rect{
    [super drawRect:rect];
    self.gradientLayer.frame = self.followBtn.bounds;
}

+(instancetype)cellWithTableView:(UITableView *)tableView{
    NSString *ID = @"MineFollowListCell";
    MineFollowListCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (!cell) {
        cell = [[MineFollowListCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    return cell;
}
- (void)setModel:(MineFollowItemModel *)model{
    _model = model;
    if ([model.userId isEqualToNumber:[RPAppManager getCustomerId]]) {
        UserInfoModel *userInfo = [UserInfoModel userInfo];
        [self.headIcon sd_setImageWithURL:[NSURL URLWithString:userInfo.imagePath] placeholderImage:[UIImage imageNamed:@"defaut_header"]];
    }else{
        [self.headIcon sd_setImageWithURL:[NSURL URLWithString:model.avatar] placeholderImage:[UIImage imageNamed:@"defaut_header"]];
    }
  
    self.nameLb.text = model.realName;
    self.descLb.text = model.userSign;
    
    if (model.followCode.intValue == 3) {
        [self.followBtn setTitle:@"互相关注" forState:UIControlStateNormal];
        [self.gradientLayer removeFromSuperlayer];
    }else if (model.followCode.intValue == 2){
        [self.followBtn setTitle:@"回关" forState:UIControlStateNormal];
        if (![self.followBtn.layer.sublayers containsObject:self.gradientLayer]) {
            [self.followBtn.layer insertSublayer:self.gradientLayer atIndex:0];
        }
    }else if (model.followCode.intValue == 1){
        [self.followBtn setTitle:@"已关注" forState:UIControlStateNormal];
        [self.gradientLayer removeFromSuperlayer];
    }else if (model.followCode.intValue == 0){
        [self.followBtn setTitle:@"关注" forState:UIControlStateNormal];
        if (![self.followBtn.layer.sublayers containsObject:self.gradientLayer]) {
            [self.followBtn.layer insertSublayer:self.gradientLayer atIndex:0];
        }
    }
}

@end
